﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using CMS.Core.DataInterfaces;
using CMS.Core.Domain;
using CMS.Data;
using AjaxPro;

namespace CMS.Presenters
{
    public class UserGroupPresenter
    {
        IUserGroupView view;
        INhomNguoiSuDungDao dataService;
        IChucNangDao cnDao;
        IVaiTroDao vtDao;
        NhomNguoiSuDung editObj;

        /// <summary>
        /// Raised when the user wants to cancel editing a object.  The containing ASPX page should 
        /// listen for this event.
        /// </summary>
        public EventHandler CancelUpdateEvent;

        /// <summary>
        /// Raised after the object has been successfully committed to the database.
        /// </summary>
        public EventHandler UpdateCompleteEvent;

        /// <summary>
        /// Raised after the object has been successfully remove from the database.
        /// </summary>
        public EventHandler DeleteCompleteEvent;

        //Attaches this presenter to the view's events.
        public UserGroupPresenter(IUserGroupView view, INhomNguoiSuDungDao dataService)
        {
            this.view = view;
            this.dataService = dataService;
            this.cnDao = NHibernateDaoFactory.Instance.GetChucNangDao();
            this.vtDao = NHibernateDaoFactory.Instance.GetVaiTroDao();
            SubscribeViewToEvents();
        }
        public UserGroupPresenter()
        {

        }

        public void InitViewForEditing(string objectId, bool isPostBack)
        {
            if (!isPostBack)
            {
                this.editObj = this.dataService.GetById(Convert.ToByte(objectId));
            }
        }

        void SubscribeViewToEvents()
        {
            view.Load += OnViewLoad;
        }

        void OnViewLoad(object sender, EventArgs e)
        {
            if (!view.IsPostBack)
            {
                //LoadViewFromModel();
                //view.DataBind();
            }
        }

        public void LoadViewFromModel(Boolean isAction, string maNSD)
        {

            view.GroupList = this.dataService.GetAllOrderField("TenNhom", true);
            //view.IsRole = NHibernateDaoFactory.Instance.GetKiemTraQuyenDao().CheckQuyenCapNhatNguoiSuDung(maNSD);
            if (isAction)
            {
                view.ChucNangList = this.cnDao.GetFieldEqualCriteria("TrangThai", true);
                view.VaiTroList = this.vtDao.GetAll();
            }
            if (this.editObj != null)
            {
                view.TenNhom = this.editObj.TenNhom;
                view.MoTa = this.editObj.MoTa;
                view.TrangThai = this.editObj.TrangThai;
                view.VaiTro = this.editObj.ChucNang;
                view.ChucNangs = this.editObj.ChucNangs;
            }
        }

        NhomNguoiSuDung ObjectToUpdate
        {
            get
            {
                NhomNguoiSuDung gObj = this.editObj;
                if (gObj == null)
                {
                    gObj = new NhomNguoiSuDung();
                }
                gObj.TenNhom = view.TenNhom;
                gObj.MoTa = view.MoTa;
                gObj.TrangThai = view.TrangThai;
                gObj.ChucNang = view.VaiTro;
                IList<ChucNang> cnList = new List<ChucNang>();
                if (view.MaChucNang != "")
                {
                    cnList = cnDao.GetListIDs(view.MaChucNang);
                }
                gObj.ChucNangs = cnList;

                return gObj;
            }
            set
            {
                if (value == null) throw new ArgumentNullException("ObjectToUpdate may not be null");
            }
        }

        public void CancelUpdate()
        {
            CancelUpdateEvent(this, null);
        }

        public void UpdateObject()
        {
            NhomNguoiSuDung gObj = this.ObjectToUpdate;
            if (this.editObj == null)
            {
                this.dataService.Save(gObj);
            }
            else
            {
                this.dataService.Update(gObj);
            }
            
            UpdateCompleteEvent(this, null);
        }

        public void ChangeObjectsStatus(String ids)
        {
            IList<NhomNguoiSuDung> gList = this.dataService.GetListIDs(ids);
            //foreach (NhomNguoiSuDung gObj in gList)
            //{
            //    //gObj.TrangThai = false;
            //    //this.dataService.Update(gObj);
            //    //this.dataService.Delete(gObj);                
            //}
            this.dataService.DeleteHQL(String.Format("FROM NhomNguoiSuDung WHERE MaNhom in ({0})", ids));
            DeleteCompleteEvent(this, null);
        }
    }
}
